package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.cqaospda.R
import com.glory.cqaospda.adapter.EqpInfoAdapter
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.bean.QueryEqpInfoBean
import com.glory.cqaospda.utils.EditTextEnterUtils
import com.glory.cqaospda.viewmodel.QueryEqpInfoModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText
import kotlinx.android.synthetic.main.activity_queryeqpinfo.*
import kotlinx.android.synthetic.main.activity_queryeqpinfo.etEqpId
import kotlinx.android.synthetic.main.activity_queryrepairinfo.*
import kotlinx.android.synthetic.main.activity_title.*
import kotlinx.android.synthetic.main.eqpinfo_layout.*
import java.lang.Exception

class QueryEqpInfoActivity : BaseActivity<QueryEqpInfoModel>() {

    var eqpInfoAdapter: EqpInfoAdapter? = null
    var list = ArrayList<QueryEqpInfoBean.TesterList>()
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@QueryEqpInfoActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    override fun initData() {

        queryEqpData(etEqpId)

        viewModel.defUI.callObserve.observe(this@QueryEqpInfoActivity) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        val eqpInfoBean = it.item as QueryEqpInfoBean
                        list.clear()
                        list.addAll(eqpInfoBean.mTesterList)
                        eqpInfoAdapter!!.setList(list)
                    }
                    2 -> {
                        eqpInfoAdapter!!.setList(null)
                    }
                }
            }else{
                Constant.okgoError = false
            }
        }

    }

    override fun initView(savedInstanceState: Bundle?) {
        tvTitle.text = "设备信息查询"
        tvUserName.text = Constant.UserName
        tvBaseSubmit.visibility = View.GONE
        initRecyclerView(rvEqpInfoList, this)
        eqpInfoAdapter = EqpInfoAdapter(null)
        rvEqpInfoList.adapter = eqpInfoAdapter

        val instance = MyApp.instance
        instance!!.handler = handler
    }

    override fun layoutId(): Int {
        return R.layout.activity_queryeqpinfo
    }

    private fun queryEqpData(
        editText: FreeUI_ClearEditText?,
    ) {
        EditTextEnterUtils.initEnterToUpperCase(editText)
        FreeApi_EditTextUtils.setEditTextListener(editText, this) { s: String? ->
            eqpInfoAdapter!!.setList(null)
            viewModel.defaultEqpInfo(s!!)
        }

    }

    protected fun initRecyclerView(recyclerView: RecyclerView, context: Context?) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addItemDecoration(DividerItemDecoration(context, 1))
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                108 -> {
                    finish()
                }
            }
        }
    }
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == 520) {
            try {
                val view = window.decorView.findFocus() as EditText
                when (view) {
                    etEqpId ->{
                        etEqpId.setText("")
                    }
                }
            }catch (e: Exception){
                println(e.message)
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}